#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,a[N],b[N],cur[N*4],top,ans,dep[N*4];
struct node{
	int r,z,z1,id;
};
vector<node>q[N*4];
queue<int>qwq; 
int dfs(int i,int z)
{
	if(i==top||!z) return z;
	int sum=0;
	for(int j=cur[i];j<q[i].size();++j)
	{
		cur[i]=j;
		if(dep[q[i][j].r]==dep[i]+1)
		{
			int sum1=dfs(q[i][j].r,min(z-sum,q[i][j].z-q[i][j].z1));
			if(sum1>0)
			{
				q[i][j].z1+=sum1;
				q[q[i][j].r][q[i][j].id].z1-=sum1;
				sum+=sum1;
				if(sum==z) return z;
			}
		}
	}
	return sum;
}
void dc()
{
	while(1)
	{
		for(int i=1;i<=top;++i)
			cur[i]=dep[i]=0;
		dep[1]=1;
		qwq.push(1);
		while(!qwq.empty())
		{
			int now=qwq.front();
			qwq.pop();
			for(int j=0;j<q[now].size();++j)
				if(!dep[q[now][j].r]&&q[now][j].z>q[now][j].z1)
					dep[q[now][j].r]=dep[now]+1,qwq.push(q[now][j].r);
		}
		if(!dep[top]) return;
		ans+=dfs(1,n);
	}
}
int main()
{
	freopen("pairint.in","r",stdin);
	freopen("pairint.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	top=2*n+1;
	for(int i=1;i<=n;++i)
	{
		cin>>a[i]>>b[i];
		++top;
		int now=q[a[i]+1].size(),now1=q[top].size();
		q[top].push_back((node){a[i]+1,1,0,now});
		q[a[i]+1].push_back((node){top,0,0,now1});
		now=q[b[i]+1].size(),now1=q[top].size();
		q[top].push_back((node){b[i]+1,1,0,now});
		q[b[i]+1].push_back((node){top,0,0,now1});
		now=q[1].size(),now1=q[top].size();
		q[top].push_back((node){1,0,0,now});
		q[1].push_back((node){top,1,0,now1});
	}
	++top;
	for(int i=1;i<=2*n;++i)
	{
		int now=q[i+1].size(),now1=q[top].size();
		q[i+1].push_back((node){top,1,0,now1});
		q[top].push_back((node){i+1,0,0,now});
	}
	dc();
	cout<<ans;
	return 0;
 } 
